package flirtme.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.widget.Toast;

import com.ans.flirtme.R;
import com.google.android.gcm.GCMRegistrar;

import flirtme.activities.LogInActivity;
import flirtme.constants.Constants;
import flirtme.managers.UserManager;

public class SettingsFragment extends PreferenceFragment {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Load the preferences from an XML resource
		addPreferencesFromResource(R.xml.preferences);
		
		setLogogOutActions();
		setRegisterGCMAction();
	
		
/*		Preference faq = (Preference) findPreference("settings_item_faq");
		Intent intent = new Intent (this.getActivity(), FaqActivity.class);
		
		faq.setIntent(intent);
		
		faq.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference preference) {
//				preference.ge
//				Intent intent = preference.getIntent();
//				preference.getContext().startActivity(intent);
//				parent.showActivity(FaqActivity.class);
//				
				return false;
			}
		});*/

	}
	
   private void setRegisterGCMAction() {
	   Preference register = (Preference) findPreference("settings_item_resend");

	   register.setOnPreferenceClickListener(new OnPreferenceClickListener() {
		@Override
		public boolean onPreferenceClick(Preference preference) {
				Toast.makeText(getActivity(), R.string.calling_service_dialog_msg,
					Toast.LENGTH_LONG).show();
				 GCMRegistrar.register(getActivity(), Constants.SENDER_ID);
				return false;
			}
		});
	  
	}

private void setLogogOutActions() {
		Preference logout = (Preference) findPreference("settings_item_logout");
		Intent intent = new Intent (this.getActivity(), LogInActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
		logout.setIntent(intent);
		
		logout.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference preference) {
				
				UserManager.instance().logout(getActivity());
				Toast.makeText(getActivity(), R.string.logged_out_message,
						Toast.LENGTH_LONG).show();
				return false;
			}
		});
		
	}


}
